require.config({
    paths: {
        vue: "../encaptool/encvue",
        iview: "../utiltool/iview/iview",
        jquery: "../utiltool/jquery/jquery.min",
        router: "../encaptool/encarouter",
        axios: "../encaptool/encaxios",
        echarts:"../utiltool/echarts/echarts.min"
    },
    shim: {
        iview: ["vue"]
    }
});
require(["vue", "iview","echarts","jquery", "router", "axios"], function (
    Vue,
    iview,
    echarts,
    $,
    Router,
    axios
) {
    Vue.use(iview);
    Vue.use(echarts);
    new Vue({
        el: "#user_management",
        data() {
            return {
                phone: '',
                searchItems: ['好像没有这个'],
                truckInfo: [],
                historyData: [],
                loading: false,
                truckCl: [
                    {
                        title: '序号',
                        type: 'index',
                        align: 'center'
                    },
                    {
                        title: '车主',
                        key: 'name',
                        align: 'center'
                    },
                    {
                        title: '车牌号',
                        key: 'truckid',
                        align: 'center'
                    },
                    {
                        title: '联系方式',
                        key: 'phone',
                        align: 'center'
                    },
                    {
                        title: '身份证号',
                        key: 'identity',
                        align: 'center'
                    },
                    {
                        title: '年龄',
                        key: 'age',
                        align: 'center'
                    },
                    {
                        title: '性别',
                        key: 'sex',
                        align: 'center'
                    },{
                        title: '近期出车时间',
                        key: 'lastdate',
                        align: 'center'
                    },
                    {
                        title: '操作',
                        key: 'action',
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'primary'
                                    },
                                    style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: () => {
                                            this.change(params.index);
                                        }
                                    }
                                }, '修改'),
                                h('Button', {
                                    props: {
                                        type: 'info'
                                    },
                                    style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: () => {
                                            this.remove(params.index);
                                        }
                                    }
                                }, '删除')
                            ]);
                        }
                    }
                ],
                user: {
                    name: '',
                    truckid: '',
                    phone: '',
                    password: '',
                    check: '',
                    identity: '',
                    age: '',
                    sex: ''
                },
                sexList: [
                    {
                        value: '男',
                        label: '男'
                    },
                    {
                        value: '女',
                        label: '女'
                    }
                ],
                truckCount: 0,
                pageSize: 50,
                modal1: false,
                showName: true,
                verifyFlag: 1,
                warnMsg: ''
            };
        },
        mounted() {
            this.getAllUser();
        },
        watch: {
            phone: 'getSearchList',
            'user.phone': 'getSearchList',
            'user.identity': 'getAge'
        },
        methods: {
            getAllUser () {
                this.loading = true;
                var setting = {
                    method: 'get',
                    url: '/YF/public/api/getAllUser'
                }
                axios.call(this, function(res){
                    if(res.code === 0){
                        this.truckInfo = res.data;
                        this.truckCount = res.data.length;
                        if(this.truckCount < this.pageSize) {
                            this.historyData = this.truckInfo;
                        }else {
                            this.historyData = this.truckInfo.slice(0, this.pageSize);
                        }
                        this.loading = false;
                    }else{
                        this.$Message.info("数据为空");
                        this.loading = false;
                    }
                }.bind(this),{
                    setting: setting
                })
            },
            changePage (index) {
                var _start = ( index - 1 ) * this.pageSize;
                var _end = index * this.pageSize;
                this.historyData = this.truckInfo.slice(_start,_end);
            },
            change (index) {
                this.$Modal.confirm({
                    render: (h) => {
                        return h('div', [
                            h('P', {}, '姓名'),
                            h('Input', {
                                props: {
                                    value: this.historyData[index].name.toString()
                                },
                                on: {
                                    input: (val) => {
                                        this.historyData[index].name = val
                                    }
                                }
                            }),
                            h('P', {}, '车牌号'),
                            h('Input', {
                                props: {
                                    value: this.historyData[index].truckid.toString()
                                },
                                on: {
                                    input: (val) => {
                                        this.historyData[index].truckid = val
                                    }
                                }
                            }),
                            h('P', {}, '联系方式'),
                            h('Input', {
                                props: {
                                    value: this.historyData[index].phone.toString(),
                                    readonly: true
                                }
                            }),
                            h('P', {}, '身份证号'),
                            h('Input', {
                                props: {
                                    value: this.historyData[index].identity.toString()
                                },
                                on: {
                                    input: (val) => {
                                        this.historyData[index].identity = val
                                    }
                                }
                            }),
                            h('P', {}, '年龄'),
                            h('Input', {
                                props: {
                                    value: this.historyData[index].age.toString()
                                },
                                on: {
                                    input: (val) => {
                                        this.historyData[index].age = val
                                    }
                                }
                            }),
                            h('P', {}, '性别'),
                            h('Input', {
                                props: {
                                    value: this.historyData[index].sex.toString()
                                },
                                on: {
                                    input: (val) => {
                                        this.historyData[index].sex = val
                                    }
                                }
                            })
                        ])
                    },
                    onOk: () => {
                        let setting = {
                            method: 'post',
                            url: '/YF/public/api/updateUser'
                        }
                        let data = {
                            phone: this.historyData[index].phone,
                            truckid: this.historyData[index].truckid,
                            name: this.historyData[index].name,
                            username: this.historyData[index].name,
                            identity: this.historyData[index].identity,
                            age: this.historyData[index].age,
                            sex: this.historyData[index].sex
                        }
                        axios.call(this, function (res) {
                            if(res.code === 0){
                                this.$Message.success('更改成功');
                                this.getAllUser();
                            }else{
                                this.$Message.info('更改失败');
                            }
                        }.bind(this),{
                            setting: setting,
                            data: data
                        })
                    },
                    onCancel: () => {
                        this.$Message.info("取消修改");
                        this.getAllUser();
                    }
                })
            },
            remove (index) {
                this.$Modal.confirm({
                    title: '删除提示窗',
                    content: '<p>确认要删除此用户么？</p>',
                    onOk: () => {
                        let setting = {
                            method: 'post',
                            url: '/YF/public/api/deleteUser'
                        }
                        axios.call(this, function (res) {
                            if(res.code === 0){
                                this.$Message.success('删除成功');
                                this.getAllUser();
                            }else{
                                this.$Message.info('删除失败');
                            }
                        }.bind(this),{
                            setting: setting,
                            data: {
                                phone: this.historyData[index].phone,
                                truckid: this.historyData[index].truckid
                            }
                        })
                    },
                    onCancel: () => {
                        this.$Message.info('取消删除');
                    }
                });
            },
            getSearchList (val, oldVal) {
                var searchSetting = {
                    method: 'GET',
                    url: "/YF/public/api/getPhoneByPhone/" + val
                }
                if (val) {
                    axios.call(this, function(res){
                        if(res.code === 0){
                            this.searchItems = res.data.slice(0,10);
                        }else{
                            this.searchItems = [];
                        }
                    }.bind(this),{
                        setting: searchSetting
                    })
                }
            },
            getSearchTruck () {
                this.loading = true;
                var setting = {
                    method: 'get',
                    url: "/YF/public/api/getSearchTruck/" + this.phone
                }
                axios.call(this, function(res){
                    if(res.code === 0){
                        this.historyData = res.data;
                        this.truckCount = res.data.length;
                    }
                    else{
                        this.$Message.info("您要查找的车辆不存在");
                    }
                    this.loading = false;
                }.bind(this),{
                    setting: setting
                })
            },
            // 导出csv
            exportData () {
                var today = new Date();
                var dateStr = today.getFullYear() + (today.getMonth() + 1).toString()
                + today.getDate() + today.getHours()+ today.getMinutes() + today.getSeconds();
                this.$refs.table.exportCsv({
                    filename: '车辆信息' + dateStr,
                    columns: this.truckCl.filter((col, index) => index > 0 && index < 7),
                    data: this.truckInfo
                });
            },
            getAge (val, oldVal) {
                if(val.length > 10){
                    let birth = val.slice(6, 10);
                    let date = new Date();
                    let today = date.getFullYear();
                    this.user.age = parseInt(today) - parseInt(birth);
                }
                else{
                    this.user.age = '正在计算年龄...'
                }
            },
            cancel () {
                this.clear();
            },
            clear () {
                this.user.name = '';
                this.user.truckid = '';
                this.user.phone = '';
                this.user.password = '';
                this.user.check = '';
                this.user.identity = '';
                this.user.age = '';
                this.user.sex = '';
            },
            isPhone (str) {
                var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
                if (!myreg.test(str)){  
                    return false;
                }else{  
                    return true;
                }
            },
            verify () {
                if(this.user.truckid == ''){
                    this.warnMsg = "车牌号不能为空";
                    this.verifyFlag = 0;
                }else if(this.user.phone == ''){
                    this.warnMsg = "手机号不能为空";
                    this.verifyFlag = 0;
                }else if(this.isPhone(this.user.phone) == false){
                    this.warnMsg = "手机号不正确";
                    this.verifyFlag = 0;
                }else if(this.user.password == '' || this.user.check == ''){
                    this.warnMsg = "密码不能为空";
                    this.verifyFlag = 0;
                }else if(this.user.password != this.user.check){
                    this.warnMsg = "密码不一致";
                    this.verifyFlag = 0;
                }else if(this.user.password.length < 6){
                    this.warnMsg = "密码太短";
                    this.verifyFlag = 0;
                }else if(this.user.password.length > 20){
                    this.warnMsg = "密码太长";
                    this.verifyFlag = 0;
                }else{
                    this.warnMsg = "添加成功";
                    this.verifyFlag = 1;
                }
            },
            addUser () {
                this.verify();
                if(this.verifyFlag == 1){
                    let setting = {
                        method: 'post',
                        url: "/YF/public/api/addUser"
                    }
                    let data = {
                        truckid: this.user.truckid,
                        name: this.user.name,
                        username: this.user.name,
                        phone: this.user.phone,
                        password: this.user.check,
                        identity: this.user.identity,
                        age: this.user.age,
                        sex: this.user.sex,
                        state: 0,
                        singlestate: 0
                    }
                    axios.call(this, function(res){
                        if(res.code == 0){
                            this.$Message.success(this.warnMsg);
                            this.getAllUser();
                        }
                    }.bind(this),{
                        setting: setting,
                        data: data
                    })
                }else{
                    this.$Message.warning(this.warnMsg);
                }
            }
        }, 
    });
});